package com.biz.primus.management.promotion.tag;

import java.io.IOException;
import java.util.List;
import java.util.stream.IntStream;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

/**
 * 营销中心会员等级选择标签
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class PromotionMemberLevelSelectTag extends TagSupport {

    private static final long serialVersionUID = 5062407768133420751L;

    private List<Integer> selectMemberLevels;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        IntStream.rangeClosed(0, 8).forEach(i -> {
            try {
                if (CollectionUtils.isNotEmpty(selectMemberLevels) && selectMemberLevels.contains(String.valueOf(i))) {
                    jspWriter.print(String.format("<option value=\"%s\" selected >%s级</option>", i, i));
                } else {
                    jspWriter.print(String.format("<option value=\"%s\">%s级</option>", i, i));
                }
            } catch (IOException e) {
                log.error("输出营销中心用户等级失败", e);
            }
        });
        return super.doStartTag();
    }
}
